﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class admin_HotelManager : System.Web.UI.Page
{
    public static int hotelID;
    public static List<string> pathLinkAdd = new List<string>();
    public static List<ClassUnity.Images> pathLink = new List<ClassUnity.Images>();


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!checkLoginAdmin())
            {
                ClientScript.RegisterStartupScript(typeof(Page), "alert", "<script> alert('You have to login as Admin to access this page!');</script>");
                Response.Redirect("Login.aspx?prePage=hotelManager");
            }
            LoadData();
            ClearImage();

            //Sort
            if (GridView1.Rows.Count > 0)
            {
                GridView1.UseAccessibleHeader = true;
                GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;
                //GridView1.FooterRow.TableSection = TableRowSection.TableFooter;
            }
            else
                ClientScript.RegisterStartupScript(typeof(Page), "alert", "<script> alert('Have not hotel!');</script>");
        }
    }
    // check login
    public bool checkLoginAdmin()
    {
        // check session
        if (Session["username"] != null)
        {
            return true;
        }
        else
        {
            // check cookie 
            if (Request.Cookies["username"] != null)
            {
                Session["username"] = Request.Cookies["username"].Value;
                Session["fullname"] = Request.Cookies["fullname"].Value;
                return true;
            }

        }
        return false;
    }
    private void LoadData()
    {
        DataTable dt = HotelManagement.GetAllHotel().Tables["AllHotel"];

        GridView1.DataSource = dt;
        GridView1.DataBind();

        MultiView1.ActiveViewIndex = 0;
    }


    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        LoadData();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        hotelID = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());

        //Delete file img in server
        DataTable dtImageDel = ImageManagement.GetImgByForeignID(hotelID, Common.IMAGE_HOTEL).Tables["AllImgByForeignID"];
        foreach (DataRow row in dtImageDel.Rows)
            File.Delete(Server.MapPath(row[ImageManagement.PATHLINK].ToString()));
        //Delete all image of hotel have hotelID= hotelID
        ImageManagement.DeleteImg(Common.IMAGE_HOTEL, hotelID);
        //Delete Tour_Hotel
        TourHotelManagement.DeleteTourHotelFromHotel(hotelID);
        //Delte hotel
        if (HotelManagement.DeleteHotel(hotelID))
            LoadData();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        if (HotelManagement.EditHotel(hotelID, txtHotelNameEdit.Text, float.Parse(txtPriceEdit.Text), int.Parse(ddlStartEdit.SelectedValue),int.Parse(txtRoomEdit.Text), txtAddressEdit.Text, txtTelEdit.Text, txtDescriptionEdit.Text))
        {
            LoadData();
            MultiView1.ActiveViewIndex = 0;
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        //Add img to pathLinkAdd
        if (Image1.ImageUrl != "")
            pathLinkAdd.Add(Image1.ImageUrl);
        if (Image2.ImageUrl != "")
            pathLinkAdd.Add(Image2.ImageUrl);
        if (Image3.ImageUrl != "")
            pathLinkAdd.Add(Image3.ImageUrl);
        if (Image4.ImageUrl != "")
            pathLinkAdd.Add(Image4.ImageUrl);
        if (Image5.ImageUrl != "")
            pathLinkAdd.Add(Image5.ImageUrl);
        if (Image6.ImageUrl != "")
            pathLinkAdd.Add(Image6.ImageUrl);
        if (Image7.ImageUrl != "")
            pathLinkAdd.Add(Image7.ImageUrl);
        if (Image8.ImageUrl != "")
            pathLinkAdd.Add(Image8.ImageUrl);
        if (Image9.ImageUrl != "")
            pathLinkAdd.Add(Image9.ImageUrl);
        if (Image10.ImageUrl != "")
            pathLinkAdd.Add(Image10.ImageUrl);

        if (HotelManagement.AddHotel(txtHotelName.Text, float.Parse(txtPrice.Text), int.Parse(ddlStart.SelectedValue),int.Parse(txtRoom.Text), txtAddress.Text, txtTel.Text, txtDescription.Text, pathLinkAdd))
        {
            LoadData();
            ClearImage();
            MultiView1.ActiveViewIndex = 0;
        }
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < pathLink.Count; i++)
            ImageManagement.EditImg(pathLink[i].pathLink, pathLink[i].imageID);
        //add images for landscape
        if (pathLinkAdd.Count > 0)
            for (int i = 0; i < pathLinkAdd.Count; i++)
                ImageManagement.AddImg(pathLinkAdd[i], Common.IMAGE_HOTEL, hotelID);

        HotelManagement.EditHotel(hotelID, txtHotelNameEdit.Text, float.Parse(txtPriceEdit.Text), int.Parse(ddlStartEdit.SelectedValue),int.Parse(txtRoomEdit.Text), txtAddressEdit.Text, txtTelEdit.Text, txtDescriptionEdit.Text);
        LoadData();

        ClearImage();
        MultiView1.ActiveViewIndex = 0;
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        ClearImage();
        MultiView1.ActiveViewIndex = 0;
    }
    protected void btnCancel2_Click(object sender, EventArgs e)
    {
        ClearImage();
        MultiView1.ActiveViewIndex = 0;
    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        txtHotelName.Text = "";
        txtPrice.Text = "";
        txtTel.Text = "";
        txtAddress.Text = "";
        txtDescription.Text = "";
        txtHotelName.Focus();

        MultiView1.ActiveViewIndex = 1;
    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        hotelID = int.Parse(GridView1.DataKeys[e.NewSelectedIndex].Value.ToString());
        DataTable dtHotetByID = HotelManagement.GetHotelByID(hotelID).Tables[0];
        if (dtHotetByID.Rows.Count > 0)
        {
            txtHotelNameEdit.Text = dtHotetByID.Rows[0][HotelManagement.HOTELNAME].ToString();
            ddlStartEdit.SelectedValue = dtHotetByID.Rows[0][HotelManagement.START].ToString();
            txtPriceEdit.Text = String.Format("{0:0.##}", float.Parse(dtHotetByID.Rows[0][HotelManagement.PRICE].ToString()));
            txtRoomEdit.Text = dtHotetByID.Rows[0][HotelManagement.ROOM].ToString();
            txtTelEdit.Text = dtHotetByID.Rows[0][HotelManagement.PHONE].ToString();
            txtAddressEdit.Text = dtHotetByID.Rows[0][HotelManagement.ADDRESS].ToString();
            txtDescriptionEdit.Text = dtHotetByID.Rows[0][HotelManagement.HOTELDESCRIPTION].ToString();
            LoadImage(hotelID);
        }

        MultiView1.ActiveViewIndex = 2;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        ImageUploadInsert();
    }
    protected void ButtonMore_Click(object sender, EventArgs e)
    {
        if (ButtonMore.Text == "Only five")
        {
            div2.Visible = false;
            ButtonMore.Text = "Add 10 Photos";
            div4.Visible = false;
        }

        else if (ButtonMore.Text == "Add 10 Photos")
        {
            div2.Visible = true;
            ButtonMore.Text = "Only five";
            div4.Visible = true;
        }
    }
    protected void btnUploadEdit_Click(object sender, EventArgs e)
    {
        ImageUploadEdit();
    }

    private void ClearImage()
    {
        Image1.ImageUrl = Image1Edit.ImageUrl = "";
        Image2.ImageUrl = Image2Edit.ImageUrl = "";
        Image3.ImageUrl = Image3Edit.ImageUrl = "";
        Image4.ImageUrl = Image4Edit.ImageUrl = "";
        Image5.ImageUrl = Image5Edit.ImageUrl = "";
        Image6.ImageUrl = Image6Edit.ImageUrl = "";
        Image7.ImageUrl = Image7Edit.ImageUrl = "";
        Image8.ImageUrl = Image8Edit.ImageUrl = "";
        Image9.ImageUrl = Image9Edit.ImageUrl = "";
        Image10.ImageUrl = Image10Edit.ImageUrl = "";
    }

    private void ImageUploadInsert()
    {
        div5.Visible = div5Edit.Visible = true;
        HttpFileCollection uploadFilCol = Request.Files;
        for (int i = 0; i < uploadFilCol.Count; i++)
        {
            HttpPostedFile file = uploadFilCol[i];
            string fileExt = Path.GetExtension(file.FileName).ToLower();
            string fileName = Path.GetFileName(file.FileName);
            if (fileName != string.Empty)
            {
                try
                {
                    if (fileExt == ".jpg" || fileExt == ".gif" || fileExt == ".bmp" || fileExt == ".jpeg" || fileExt == ".png")
                    {
                        file.SaveAs(Server.MapPath("~/img/hotels/") + fileName);
                        string linkImage = "~/img/hotels/" + fileName;
                        //pathLinkAdd.Add(linkImage);

                        if (i == 0)
                        {
                            Image1.ImageUrl = linkImage;
                            continue;
                        }
                        if (i == 1)
                        {
                            Image2.ImageUrl = linkImage;
                            continue;
                        }
                        if (i == 2)
                        {
                            Image3.ImageUrl = linkImage;
                            continue;
                        }
                        if (i == 3)
                        {
                            Image4.ImageUrl = linkImage;
                            continue;
                        }
                        if (i == 4)
                        {
                            Image5.ImageUrl = linkImage;
                            continue;
                        }
                        if (i == 5)
                        {
                            Image6.ImageUrl = linkImage;
                            continue;
                        }
                        if (i == 6)
                        {
                            Image7.ImageUrl = linkImage;
                            continue;
                        }
                        if (i == 7)
                        {
                            Image8.ImageUrl = linkImage;
                            continue;
                        }
                        if (i == 8)
                        {
                            Image9.ImageUrl = linkImage;
                            continue;
                        }
                        if (i == 9)
                        {
                            Image10.ImageUrl = linkImage;
                            continue;
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
    }

    private void ImageUploadEdit()
    {
        div5.Visible = div5Edit.Visible = true;
        HttpFileCollection uploadFilCol = Request.Files;
        for (int i = 0; i < uploadFilCol.Count; i++)
        {
            HttpPostedFile file = uploadFilCol[i];
            string fileExt = Path.GetExtension(file.FileName).ToLower();
            string fileName = Path.GetFileName(file.FileName);
            if (fileName != string.Empty)
            {
                try
                {
                    if (fileExt == ".jpg" || fileExt == ".gif" || fileExt == ".bmp" || fileExt == ".jpeg" || fileExt == ".png")
                    {
                        file.SaveAs(Server.MapPath("~/img/hotels/") + fileName);
                        string linkImage = "~/img/hotels/" + fileName;

                        if (i == 0)
                        {
                            if (Image1Edit.ImageUrl != "")
                            {
                                if (pathLink[i] != null)
                                    pathLink[i].pathLink = linkImage;
                            }
                            else
                                pathLinkAdd.Add(linkImage);
                            Image1Edit.ImageUrl = linkImage;
                            continue;
                        }
                        if (i == 1)
                        {
                            if (Image2Edit.ImageUrl != "")
                            {
                                if (pathLink[i] != null)
                                    pathLink[i].pathLink = linkImage;
                            }
                            else
                                pathLinkAdd.Add(linkImage);
                            Image2Edit.ImageUrl = linkImage;
                            continue;
                        }
                        if (i == 2)
                        {
                            if (Image3Edit.ImageUrl != "")
                            {
                                if (pathLink[i] != null)
                                    pathLink[i].pathLink = linkImage;
                            }
                            else
                                pathLinkAdd.Add(linkImage);
                            Image3Edit.ImageUrl = linkImage;
                            continue;
                        }
                        if (i == 3)
                        {
                            if (Image4Edit.ImageUrl != "")
                            {
                                if (pathLink[i] != null)
                                    pathLink[i].pathLink = linkImage;
                            }
                            else
                                pathLinkAdd.Add(linkImage);
                            Image4Edit.ImageUrl = linkImage;
                            continue;
                        }
                        if (i == 4)
                        {
                            if (Image5Edit.ImageUrl != "")
                            {
                                if (pathLink[i] != null)
                                    pathLink[i].pathLink = linkImage;
                            }
                            else
                                pathLinkAdd.Add(linkImage);
                            Image5Edit.ImageUrl = linkImage;
                            continue;
                        }
                        if (i == 5)
                        {
                            if (Image6Edit.ImageUrl != "")
                            {
                                if (pathLink[i] != null)
                                    pathLink[i].pathLink = linkImage;
                            }
                            else
                                pathLinkAdd.Add(linkImage);
                            Image6Edit.ImageUrl = linkImage;
                            continue;
                        }
                        if (i == 6)
                        {
                            if (Image7Edit.ImageUrl != "")
                            {
                                if (pathLink[i] != null)
                                    pathLink[i].pathLink = linkImage;
                            }
                            else
                                pathLinkAdd.Add(linkImage);
                            Image7Edit.ImageUrl = linkImage;
                            continue;
                        }
                        if (i == 7)
                        {
                            if (Image8Edit.ImageUrl != "")
                            {
                                if (pathLink[i] != null)
                                    pathLink[i].pathLink = linkImage;
                            }
                            else
                                pathLinkAdd.Add(linkImage);
                            Image8Edit.ImageUrl = linkImage;
                            continue;
                        }
                        if (i == 8)
                        {
                            if (Image9Edit.ImageUrl != "")
                            {
                                if (pathLink[i] != null)
                                    pathLink[i].pathLink = linkImage;
                            }
                            else
                                pathLinkAdd.Add(linkImage);
                            Image9Edit.ImageUrl = linkImage;
                            continue;
                        }
                        if (i == 9)
                        {
                            if (Image10Edit.ImageUrl != "")
                            {
                                if (pathLink[i] != null)
                                    pathLink[i].pathLink = linkImage;
                            }
                            else
                                pathLinkAdd.Add(linkImage);
                            Image10Edit.ImageUrl = linkImage;
                            continue;
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
    }

    private void LoadImage(int landID)
    {
        pathLink.Clear();
        pathLinkAdd.Clear();
        DataTable dtImageByLandID = ImageManagement.GetImgByForeignID(landID, Common.IMAGE_HOTEL).Tables["AllImgByForeignID"];
        for (int i = 0; i < dtImageByLandID.Rows.Count; i++)
        {
            ClassUnity.Images img = new ClassUnity.Images();
            img.imageID = int.Parse(dtImageByLandID.Rows[i][ImageManagement.IMAGEID].ToString());
            img.pathLink = dtImageByLandID.Rows[i][ImageManagement.PATHLINK].ToString();
            pathLink.Add(img);
        }

        //Fill pathLink into ...
        //FileUpload1Edit
        if (pathLink.Count > 5)
        {
            div2Edit.Visible = true;
            div4Edit.Visible = true;
        }
        else
        {
            div2Edit.Visible = false;
            div4Edit.Visible = false;
        }
        div5Edit.Visible = true;

        if (0 < pathLink.Count) if (pathLink[0] != null)
                Image1Edit.ImageUrl = pathLink[0].pathLink;
        if (1 < pathLink.Count) if (pathLink[1] != null)
                Image2Edit.ImageUrl = pathLink[1].pathLink;
        if (2 < pathLink.Count) if (pathLink[2] != null)
                Image3Edit.ImageUrl = pathLink[2].pathLink;
        if (3 < pathLink.Count) if (pathLink[3] != null)
                Image4Edit.ImageUrl = pathLink[3].pathLink;
        if (4 < pathLink.Count) if (pathLink[4] != null)
                Image5Edit.ImageUrl = pathLink[4].pathLink;
        if (5 < pathLink.Count) if (pathLink[5] != null)
                Image6Edit.ImageUrl = pathLink[5].pathLink;
        if (6 < pathLink.Count) if (pathLink[6] != null)
                Image7Edit.ImageUrl = pathLink[6].pathLink;
        if (7 < pathLink.Count) if (pathLink[7] != null)
                Image8Edit.ImageUrl = pathLink[7].pathLink;
        if (8 < pathLink.Count) if (pathLink[8] != null)
                Image9Edit.ImageUrl = pathLink[8].pathLink;
        if (9 < pathLink.Count) if (pathLink[9] != null)
                Image10Edit.ImageUrl = pathLink[9].pathLink;
    }
    protected void btnButtonMoreEdit_Click(object sender, EventArgs e)
    {
        if (btnButtonMoreEdit.Text == "Only five")
        {
            div2Edit.Visible = false;
            btnButtonMoreEdit.Text = "Add 10 Photos";
            div4Edit.Visible = false;
        }

        else if (btnButtonMoreEdit.Text == "Add 10 Photos")
        {
            div2Edit.Visible = true;
            btnButtonMoreEdit.Text = "Only five";
            div4Edit.Visible = true;
        }
    }
}